{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d8c3c45c",
   "metadata": {},
   "source": [
    "# 1.人脸比对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "52defd93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679282677,3e724f6d-f8f9-4492-93a9-7e26b62959d7', 'time_used': 451, 'confidence': 70.688, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'c78d479a6f1ab7418aaf570278f59271', 'face_rectangle': {'top': 91, 'left': 42, 'width': 145, 'height': 145}}], 'faces2': [{'face_token': '16e454f75cba878a014bbff0c860f6e6', 'face_rectangle': {'top': 679, 'left': 192, 'width': 428, 'height': 428}}], 'image_id1': 'Q/v/AaqSwtxlv65xWwlwiw==', 'image_id2': 'g4a05oWtHzBPmurCb5xxHg=='}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f_1 = open('1.jpg', 'rb')\n",
    "f_2 = open('2.jpg', 'rb')\n",
    "img_1 = base64.b64encode(f_1.read())\n",
    "img_2 = base64.b64encode(f_2.read())\n",
    "params = {\n",
    "    \"image_base64_1\":img_1,\n",
    "    \"image_base64_2\":img_2,\n",
    "     'api_key':'Jf_a6CJW0Y2iF5_d1yCVGsuFuO-VH0bM',\n",
    "     'api_secret':'bjFCWXf_4bCXl2jklNAOegBup_D8lXm2',\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14e9581d",
   "metadata": {},
   "source": [
    "# 2.人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1038c2b6",
   "metadata": {},
   "source": [
    "## 2.1人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15ea4832",
   "metadata": {},
   "source": [
    "### 2.11创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e4d89d06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': 'd3fe990833c54e72ff12699b41081f40', 'time_used': 196, 'face_count': 0, 'face_added': 0, 'request_id': '1679283117,387deced-9dff-4e41-97f5-19a4c8c5650d', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "创建人脸库API\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "#请求参数\n",
    "params = {\n",
    "     'api_key':'Jf_a6CJW0Y2iF5_d1yCVGsuFuO-VH0bM',\n",
    "     'api_secret':'bjFCWXf_4bCXl2jklNAOegBup_D8lXm2',\n",
    "     'display_name':'什么名字',\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acf390df",
   "metadata": {},
   "source": [
    "### 2.1.2检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6400b27b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': 'd3fe990833c54e72ff12699b41081f40', 'tags': '', 'time_used': 243, 'user_data': '', 'display_name': '什么名字', 'face_tokens': [], 'face_count': 0, 'request_id': '1679283366,4be0c978-c50a-4f83-bb45-cc9e37483a70', 'outer_id': ''}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "获取人脸库信息API\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "#请求参数\n",
    "params = {\n",
    "     'api_key':'Jf_a6CJW0Y2iF5_d1yCVGsuFuO-VH0bM',\n",
    "     'api_secret':'bjFCWXf_4bCXl2jklNAOegBup_D8lXm2',\n",
    "     'faceset_token':'d3fe990833c54e72ff12699b41081f40'\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c434c625",
   "metadata": {},
   "source": [
    "### 2.1.3添加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1b5d08d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679287914,60fc8f22-225c-4baf-b0af-d5b1ef591ab4', 'time_used': 176, 'faces': [{'face_token': 'e07375c53337638033cba346680cc60c', 'face_rectangle': {'top': 679, 'left': 192, 'width': 428, 'height': 428}}], 'image_id': 'g4a05oWtHzBPmurCb5xxHg==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# 先检测人脸信息\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测:gg,lyf,wjk\n",
    "'''\n",
    "\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('1栋/wjk.jpg','rb')\n",
    "img = base64.b64encode(f.read())\n",
    "params = {\n",
    "     'api_key':'Jf_a6CJW0Y2iF5_d1yCVGsuFuO-VH0bM',\n",
    "     'api_secret':'bjFCWXf_4bCXl2jklNAOegBup_D8lXm2',\n",
    "     'image_base64':img,\n",
    "}\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d6ab968",
   "metadata": {},
   "outputs": [],
   "source": [
    "'762bc44fe7c8ede442694b8167456515', '154ed3a2b31dd9c7d909c60bd34cbb14', 'e07375c53337638033cba346680cc60c'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8146e90f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': 'd3fe990833c54e72ff12699b41081f40', 'time_used': 556, 'face_count': 3, 'face_added': 1, 'request_id': '1679287949,20b81581-a788-4891-a01d-6898a18c79ba', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "\n",
    "'''\n",
    "添加人脸API\n",
    "'''\n",
    "\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('1栋/gg.jpg','rb')\n",
    "img = base64.b64encode(f.read())\n",
    "params = {\n",
    "     'api_key':'Jf_a6CJW0Y2iF5_d1yCVGsuFuO-VH0bM',\n",
    "     'api_secret':'bjFCWXf_4bCXl2jklNAOegBup_D8lXm2',\n",
    "     \"faceset_token\":'d3fe990833c54e72ff12699b41081f40',\n",
    "     \"face_tokens\": 'e07375c53337638033cba346680cc60c'\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
